import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/11/18
 * Time: 23:03
 */
public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> ret = new ArrayList<>();
        int top = 0,bottom = matrix.length - 1,left = 0,right = matrix[0].length - 1;
        while(top <= bottom && left <= right){
            for(int i = left;i <= right;i++){
                ret.add(matrix[top][i]);
            }
            for(int i = top + 1;i <= bottom;i++){
                ret.add(matrix[i][right]);
            }
            if(top < bottom){
                for(int i = right - 1;i >= left;i--){
                    ret.add(matrix[bottom][i]);
                }
            }
            if(left < right){
                for(int i = bottom - 1;i > top;i--){
                    ret.add(matrix[i][left]);
                }
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return ret;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        List<Integer> integers = solution.spiralOrder(new int[][]{{7}, {9}, {6}});
        System.out.println(integers);
    }
}
